package com.ftdigital.primemovers_operators.util.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import com.ftdigital.primemovers_operators.R;

/**
 * Created by Curry on 2016-1-17.
 */
public class ReflectImg extends View {
    public ReflectImg(Context context) {
        super(context);
    }

    public ReflectImg(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    Bitmap srcBitmap;
    Matrix matrix;
    Bitmap refectBitmap;
    Paint paint;
    PorterDuffXfermode porterDuffXfermode;

    private void init() {
        srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.animal);
        matrix = new Matrix();
        matrix.setScale(1F, -1F);//设置图片翻转
        refectBitmap = Bitmap.createBitmap(
                srcBitmap, 0,
                0,
                srcBitmap.getWidth(),
                srcBitmap.getHeight(), matrix, true);//创建翻转图片
        paint = new Paint();
        paint.setShader(new LinearGradient(0, srcBitmap.getHeight(), 0, srcBitmap.getHeight() + srcBitmap.getHeight() / 2,
                0xDD000000, 0x10000000, Shader.TileMode.CLAMP));//设置着色器， LinearGradient 设置渐进颜色
        porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(srcBitmap, 0, 0, null);
        canvas.drawBitmap(refectBitmap, 0, srcBitmap.getHeight(), null);
        paint.setXfermode(porterDuffXfermode);//设置遮挡
        canvas.drawRect(0, srcBitmap.getHeight(), srcBitmap.getWidth(), srcBitmap.getHeight() * 2, paint);
        paint.setXfermode(null);//初始化
    }
}
